package com.xsmart.oa.services.mall.mymenu;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.common.services.LoginTokenService;
import com.xsmart.oa.dao.mall.mymenu.UserLoginDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserLoginService {
    @Autowired
    UserLoginDao userLoginDao;
    @Autowired
    LoginTokenService loginTokenService;

    public ResponseBean login(RequestBean requestBean) {
        String userName = requestBean.getRequestParamString("userName");
        String password = requestBean.getRequestParamString("password");
        String remember = requestBean.getRequestParamString("remember");
        boolean isRemember = "true".equals(remember);
        Map<String, Object> user = userLoginDao.login(userName, password);
        user.remove("password");
        String token = loginTokenService.generalTokenStr();
        loginTokenService.saveLoginToken(token, user, isRemember);
        ResponseBean responseBean = requestBean.getResponseBean();
        if (user != null) {
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("userInfo", user);
            responseBean.setToken(token);
        } else {
            responseBean.setResult(ResultConstant.USER_PASSWORD_INVALID);
        }
        return responseBean;
    }
}
